package com.remoter.basic.util;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author			koko
 * @date 			2017-08-10 10:09:17
 * @description 	类功能说明
 */
public class ConcurrentTwoKeyTwoValueHashMap<KA,KB,VA,VB>{
	
	public static class ValueHolder<VA,VB>{
		private volatile VA a;
		private volatile VB b;
		public VA getA() {
			return a;
		}
		public void setA(VA a) {
			this.a = a;
		}
		public VB getB() {
			return b;
		}
		public void setB(VB b) {
			this.b = b;
		}
		@Override
		public String toString() {
			return String.format("{a=%s,b=%s}",this.a,this.b);
		}
	}
	
	private ConcurrentHashMap<KA,ConcurrentHashMap<KB,ValueHolder<VA,VB>>> maps = new ConcurrentHashMap<KA,ConcurrentHashMap<KB,ValueHolder<VA,VB>>>();
	
	public ConcurrentTwoKeyTwoValueHashMap(){
		this.maps.clear();
	}
	
	public ConcurrentTwoKeyTwoValueHashMap<KA,KB,VA,VB> set(KA ka,KB kb,VA va,VB vb){
		if(null == ka || null == kb){
			throw new IllegalArgumentException("key is not null");
		}
		ConcurrentHashMap<KB,ValueHolder<VA,VB>> child = maps.get(ka);
		if(null == child){
			maps.put(ka,new ConcurrentHashMap<KB,ValueHolder<VA,VB>>());
			child = maps.get(ka);
		}
		ValueHolder<VA,VB> holder = child.get(kb);
		if(null == holder){
			child.put(kb,new ValueHolder<VA,VB>());
			holder = child.get(kb);
		}
		if(null != va){
			holder.setA(va);
		}
		if(null != vb){
			holder.setB(vb);
		}
		return this;
	}
	
	public ConcurrentTwoKeyTwoValueHashMap<KA,KB,VA,VB> set(KA ka,KB kb){
		return this.set(ka, kb, null, null);
	}
	
	public ConcurrentTwoKeyTwoValueHashMap<KA,KB,VA,VB> set(KA ka,KB kb,VA va){
		return this.set(ka, kb, va, null);
	}
	
	public ConcurrentTwoKeyTwoValueHashMap<KA,KB,VA,VB> del(KA ka,KB kb,VA va,VB vb){
		if(null == ka || null == kb){
			throw new IllegalArgumentException("key is not null");
		}
		ValueHolder<VA, VB> holder = this.getValueHolder(ka, kb);
		if(null != holder){
			if(holder.getA() != null && holder.getA().equals(va)){
				holder.setA(null);
			}
			if(holder.getB() != null && holder.getB().equals(vb)){
				holder.setB(null);
			}
		}
		return this;
	}
	
	public ConcurrentTwoKeyTwoValueHashMap<KA,KB,VA,VB> del(KA ka,KB kb){
		if(this.maps.containsKey(ka) && this.maps.get(ka).containsKey(kb)){
			this.maps.get(ka).remove(kb);
		}
		return this;
	}
	
	public ConcurrentTwoKeyTwoValueHashMap<KA,KB,VA,VB> del(KA ka){
		if(this.maps.containsKey(ka)){
			this.maps.remove(ka);
		}
		return this;
	}
	
	public ValueHolder<VA, VB> getValueHolder(KA ka,KB kb){
		if(maps.containsKey(ka) && maps.get(ka).containsKey(kb)){
			return maps.get(ka).get(kb);
		}else{
			return null;
		}
	}
	
	public VA getValueA(KA ka,KB kb){
		ValueHolder<VA, VB> holder = this.getValueHolder(ka, kb);
		if(null != holder){
			return holder.getA();
		}else{
			return null;
		}
	}
	
	public VB getValueB(KA ka,KB kb){
		ValueHolder<VA, VB> holder = this.getValueHolder(ka, kb);
		if(null != holder){
			return holder.getB();
		}else{
			return null;
		}
	}
	
	public boolean isEmpty(){
		return this.maps.isEmpty();
	}
	
	public boolean isKeyAEmpty(KA ka){
		if(this.maps.containsKey(ka)){
			return this.maps.get(ka).isEmpty();
		}else{
			return true;
		}
	}
	
	public boolean containsKey(KA ka){
		return this.maps.containsKey(ka);
	}
	
	public boolean containsKey(KA ka,KB kb){
		if(this.containsKey(ka)){
			return this.maps.get(ka).containsKey(kb);
		}else{
			return false;
		}
	}
	
	public ConcurrentTwoKeyTwoValueHashMap<KA,KB,VA,VB> clear(){
		this.maps.clear();
		return this;
	}
	
	public Set<Map.Entry<KA,ConcurrentHashMap<KB,ValueHolder<VA,VB>>>> entrySet(){
		return this.maps.entrySet();
	}
	
	@Override
	public String toString() {
		return this.maps.toString();
	}
	
}